package com.zhangyt.client.factory;

import com.zhangyt.client.conf.MqttProps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;

/**
 * author: Imooc
 * description: MQTT 工厂类
 * date: 2024
 */


@Configuration
/* **********************
 *
 * @EnableIntegration的作用：
 *
 * 能够扫描到@ServiceActivator注解
 *
 * *********************/
@Slf4j
public class FactoryBuilder {

    @Resource
    private MqttProps props;


    /**
     * author: Imooc
     * description: 获取Mqtt客户端
     * @param :
     * @return org.springframework.integration.mqtt.core.MqttPahoClientFactory
     */
    @Bean
    public MqttPahoClientFactory getFactory() {

        /* **********************
         *
         * 为什么要使用工厂模式创建对象
         *
         * 1. 对象创建和使用分开
         * 2. 方便维护
         *
         *
         * *********************/

        DefaultMqttPahoClientFactory client =
                new DefaultMqttPahoClientFactory();

        MqttConnectOptions options = props.getOptions();
        //配置连接地址
        options.setServerURIs(new String[]{props.getHost()});

        client.setConnectionOptions(options);

        return client;

    }





}
